<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="租赁客户" prop="customerName">
        <el-input v-model="queryParams.customerName"
                  placeholder="请输入租赁客户"
                  clearable
                  @keyup.enter.native="handleQuery"
        ></el-input>
      </el-form-item>
      <el-form-item label="租赁资产" prop="assetName">
        <el-input v-model="queryParams.assetName"
                  placeholder="请输入租赁资产"
                  clearable
                  @keyup.enter.native="handleQuery"
        ></el-input>
      </el-form-item>
      <el-form-item label="租赁开始日期" prop="leaseStartDate">
        <el-date-picker clearable
          v-model="queryParams.leaseStartDate"
          type="date"
          style="width: 205px"
          value-format="yyyy-MM-dd"
          placeholder="请选择租赁开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="租赁结束日期" prop="leaseEndDate">
        <el-date-picker clearable
          v-model="queryParams.leaseEndDate"
          type="date"
          style="width: 205px"
          value-format="yyyy-MM-dd"
          placeholder="请选择租赁结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="资产面积(㎡)" prop="assetArea">
        <el-select v-model="queryParams.assetArea" placeholder="请选择资产面积" clearable>
          <el-option
            v-for="dict in dict.type.screen_asset_area"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="租赁状态" prop="leaseStatus">
        <el-select v-model="queryParams.leaseStatus" placeholder="请选择租赁状态" clearable>
          <el-option
            v-for="dict in dict.type.lease_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="租金状态" prop="leaseStatus">
        <el-select v-model="queryParams.leaseStatus" placeholder="请选择租赁状态" clearable>
          <el-option
            v-for="dict in dict.type.rental_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:leaseInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leaseInfoList" @selection-change="handleSelectionChange" @row-click="handleRowClick">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="60" type="index" align="center" prop="leaseId" />
      <el-table-column label="合同编号" width="120" align="center" prop="leaseCode" ></el-table-column>
      <el-table-column label="承租方" width="180"  align="center" prop="customerName" />
      <el-table-column label="资产名称" show-overflow-tooltip align="center" prop="assetName" />
      <el-table-column label="资产面积(㎡)" width="140" align="center" prop="assetArea" />
      <el-table-column label="租赁日期" align="center" prop="leaseStartDate" width="240">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaseStartDate, '{y}-{m}-{d}') }}至{{parseTime(scope.row.leaseEndDate, '{y}-{m}-{d}')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁状态"  width="100" align="center" prop="leaseStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lease_status" :value="scope.row.leaseStatus"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租赁台账管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="租赁资产" prop="leaseAssetId">
              <el-select v-model="form.leaseAssetId" @change="changeAsset" style="width: 100%" placeholder="请选择租赁资产" clearable>
                <el-option
                  v-for="asset in assetList"
                  :key="asset.assetId"
                  :label="asset.assetName"
                  :value="asset.assetId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="租赁客户" prop="leaseCustomerId">
              <el-select v-model="form.leaseCustomerId" @change="changeCustomer" style="width: 100%" placeholder="请选择租赁客户" clearable>
                <el-option
                  v-for="customer in customerList"
                  :key="customer.customerId"
                  :label="customer.customerName"
                  :value="customer.customerId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="租赁开始日期" prop="leaseStartDate">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.leaseStartDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择租赁开始日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="租赁结束日期" prop="leaseEndDate">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.leaseEndDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择租赁结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="租金" prop="leaseRent">
              <el-input v-model="form.leaseRent" clearable placeholder="租金" >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="leaseRemark">
          <el-input v-model="form.leaseRemark" :rows="4" resize="none"  type="textarea" placeholder="备注" />
        </el-form-item>
        <el-form-item label="租赁照片" prop="leaseImg">
          <image-upload v-model="form.leaseImg" :limit="10"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLeaseInfo, getLeaseInfo, delLeaseInfo, addLeaseInfo, updateLeaseInfo, getLeaseInfoEdit } from "@/api/business/leaseInfo";
import { getAssetInfoList } from '@/api/business/assetInfo'
import { getCustomerInfoList } from '@/api/business/customerInfo'
import {proCityList} from '@/utils/china'
export default {
  name: "LeaseInfo",
  dicts: ['lease_status', 'screen_asset_area', 'rental_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租赁台账管理表格数据
      leaseInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      assetList: [],
      customerList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        leaseCustomerId: [
          { required: true, message: "请选择租赁客户", trigger: "blur" }
        ],
        leaseAssetId: [
          { required: true, message: "请选择租赁资产", trigger: "blur" }
        ],
        leaseStartDate: [
          { required: true, message: "租赁开始日期不能为空", trigger: "blur" }
        ],
        leaseEndDate: [
          { required: true, message: "租赁结束日期不能为空", trigger: "blur" }
        ],
        leaseRent: [
          { required: true, message: "租金s不能为空", trigger: "blur" }
        ]
      },
      proCityList: proCityList,
      cityLsit: [],
      areaList: [],
    };
  },
  created() {
    this.getInfoData()
    this.getList();
  },
  methods: {
    getInfoData() {
      getAssetInfoList().then(r => {
        this.assetList = r.data
      })
      getCustomerInfoList().then(r => {
        this.customerList = r.data
      })
    },
    /** 查询租赁台账管理列表 */
    getList() {
      this.loading = true;
      listLeaseInfo(this.queryParams).then(response => {
        this.leaseInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        leaseId: null,
        leaseCustomerId: null,
        leaseAssetId: null,
        leaseCode: null,
        leaseAssetName: null,
        leaseCustomerName: null,
        leaseStartDate: null,
        leaseEndDate: null,
        leaseRent: null,
        leaseRemark: null,
        leaseImg: null,
        leaseStatus: null,
        leasePastRecord: null,
        leaseDate: null,
        leaseIsDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.leaseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 行点击
    handleRowClick(row, column, event) {
      if(column.label != '操作') {
        this.$router.push('/LeaseLedger/lease-info/' + row.leaseId)
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租赁台账管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const leaseId = row.leaseId || this.ids
      getLeaseInfoEdit(leaseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改租赁台账管理";
      });
    },
    handleCopy(row) {
      this.reset();
      const leaseId = row.leaseId || this.ids
      getLeaseInfoEdit(leaseId).then(response => {
        this.form = response.data;
        delete this.form.leaseId
        delete this.form.leaseDate
        delete this.form.leaseIsDelete
        this.open = true;
        this.title = "添加租赁台账管理";
      });
    },
    showInfo({leaseId}) {
      this.$router.push('/LeaseLedger/lease-info/' + leaseId)
    },
    changeAsset(val) {
      if(val) {
        this.form.leaseAssetName = this.assetList.find(i => i.assetId == val).assetName
      } else {
        this.form.leaseAssetName = ''
      }
    },
    changeCustomer(val) {
      if(val) {
        this.form.leaseCustomerName = this.customerList.find(i => i.customerId == val).customerName
      } else {
        this.form.leaseCustomerName = ''
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.leaseId != null) {
            updateLeaseInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLeaseInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const leaseIds = row.leaseId || this.ids;
      this.$modal.confirm('是否确认删除租赁台账管理编号为"' + leaseIds + '"的数据项？').then(function() {
        return delLeaseInfo(leaseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/leaseInfo/export', {
        ...this.queryParams
      }, `leaseInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
